<template>
    <div class="package-list" v-loading="loading">
        <div class="input-box">
            <el-input
                placeholder="应用名称搜索"
                style="width:334px;"
                @keyup.enter.native="getList(1)"
                clearable
                v-model.trim="name">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <el-input
                placeholder="package搜索，如（duodian.qugame.huawei）"
                style="width:360px;margin-left:20px"
                @keyup.enter.native="getList(1)"
                clearable
                v-model.trim="packageName">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>

            <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
        </div>
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">是否已处理</span>
            <el-select
                @change="getList(1)"
                style="width:90px;"
                v-model="flag">
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="未处理" :value="0"></el-option>
                <el-option label="已处理" :value="1"></el-option>
            </el-select>
            <span class="input-title">应用类型</span>
            <el-select
                @change="getList(1)"
                style="width:80px;"
                v-model="type">
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="未知" :value="0"></el-option>
                <el-option label="应用" :value="1"></el-option>
                <el-option label="游戏" :value="2"></el-option>
            </el-select>
            <span class="input-title">来源</span>
            <el-select
                @change="getList(1)"
                style="width:105px;"
                v-model="userAdd">
                <el-option label="全部" :value="0"></el-option>
                <el-option label="用户添加" :value="1"></el-option>
                <el-option label="客户回包" :value="2"></el-option>
            </el-select>
            <span class="input-title">时间段</span>
            <el-date-picker
                @change="getList(1)"
                v-model="date"
                style="width:280px"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>
        </div>
        <div class="table-box">
            <el-table
                :data="list"
                border
                style="width: 100%">
                <el-table-column
                    fixed
                    width="50"
                    prop="appId"
                    label="id">
                </el-table-column>
                <el-table-column
                    width="260"
                    label="包信息">
                    <template slot-scope="scope">
                        <div class="app-top-box">
                            <div class="app-icon" v-if="scope.row.icon">
                                <img :src="scope.row.icon" alt="">
                            </div>
                            <div style="flex:1;">{{scope.row.name || '-'}}</div>
                        </div>
                        <div class="app-bottom-box">
                            <div>{{scope.row.packageName || '-'}}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="80"
                    label="设备数">
                    <template slot-scope="scope">
                        <div>{{scope.row.userNum || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="是否已处理">
                    <template slot-scope="scope">
                        <div v-if="scope.row.flag">已处理</div>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="应用类型">
                    <template slot-scope="scope">
                        <div v-if="scope.row.type === 0">未知</div>
                        <div v-else-if="scope.row.type === 1">应用</div>
                        <div v-else-if="scope.row.type === 2">游戏</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="createTime"
                    label="创建时间">
                </el-table-column>
                <el-table-column
                    prop="updateTime"
                    label="修改时间">
                </el-table-column>
                <el-table-column
                    label="来源">
                    <template slot-scope="scope">
                        <span v-if="scope.row.userAdd === 1">用户添加</span>
                        <span v-else-if="scope.row.userAdd === 2">客户回包</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column
                    fixed="right"
                    label="更改类型">
                    <template slot-scope="scope">
                        <div><el-button v-if="scope.row.type !== 1" @click="setType(scope.row, 1)" type="text" size="small" style="color:#E6A23C;">应用</el-button></div>
                        <div><el-button v-if="scope.row.type !== 2" @click="setType(scope.row, 2)" type="text" size="small" style="color:#67C23A;">游戏</el-button></div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            loading: false,
            list: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            name: '', // 应用名称
            packageName: '', // 包名
            userAdd: 0, // 0为全部 1为用户添加 2为客户回包
            flag: 0, // 应用是否已做标记处理，0未处理，1已处理
            type: -1, // 应用类型0未知,1:应用;2游戏
            date: [] // 时间
        }
    },
    created () {
        this.getList(1)
    },

    methods: {
        async getList (num) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num

            let params = {
                userAdd: this.userAdd,
                pageIndex: this.pageNum,
                pageNum: this.pageSize
            }

            if (this.date && this.date.length) {
                params.startDate = this.date[0] + ' 00:00:00'
                params.endDate = this.date[1] + ' 23:59:59'
            }
            if (this.name) {
                params.name = this.name
            }
            if (this.packageName) {
                params.packageName = this.packageName
            }
            if (this.flag !== -1) {
                params.flag = this.flag
            }
            if (this.type !== -1) {
                params.type = this.type
            }
            let res = await this.$http.post('/app/repositoryData', params)
            if (res.code === '0') {
                this.list = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        beforeSetType (item, type) {
            let msg = `是否将改App更改为 ${type === 1 ? '应用' : '游戏'}`
            this.$confirm(msg, '更改类型', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.setType(item, type)
            }).catch(() => {
            })
        },

        // 设置类型 type 1:应用;2游戏
        async setType (item, type) {
            let res = await this.$http.post('/app/updateAppType', {
                appId: item.appId,
                type
            })
            if (res.code === '0') {
                item.flag = 1
                item.type = type
                item.updateTime = formatDate(new Date(), 'YYYY-MM-DD')
                this.$message({
                    type: 'success',
                    message: '设置成功'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        },

        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        pageChange (val) {
            this.getList(val)
        }
    }
}
</script>

<style lang="scss" scoped>
    .package-list {
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px;
            }
            .btn {
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
            .app-top-box {
                display: flex;
                align-items: center;
                .app-icon {
                    width: 50px;
                    height: 50px;
                    text-align: center;
                    border-radius: 6px;
                    overflow: hidden;
                    margin-right: 10px;
                    > img {
                        width: 100%;
                        height: 100%;
                    }
                }
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
